#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

const char * DATASECTION = "DATA";
const char * USERSECTION = "USER";
const char * ENDSECTION = "END";
enum SECTION { NONE, DATA, USER };

vector<string> &split(const string &s, char delim, vector<string> &elems) {
  stringstream ss(s);
  string item;
  while(getline(ss, item, delim))
  {
    elems.push_back(item);
  }
  return elems;
}

vector<string> split(const string &s, char delim)
{
  vector<string> elems;
  return split(s, delim, elems);
}

void print_parts(vector<string> v)
{
  vector<string>::iterator i;
  cout << "num tokens: " << v.size() << endl;
  for (i=v.begin(); i<v.end(); i++)
  {
    cout << *i << " ";
  }
  cout << endl;
}

int main()
{
  const char* fname = "test.txt";
  SECTION section = NONE;
  ifstream f(fname);
  string s;
  vector<string> v;

  while (!f.eof())
  {
    getline(f,s);
    if (s.size() != 0)
    {
      if (s == ENDSECTION)
        section = NONE;
      else if (s == USERSECTION)
        section = USER;
      else if (s == DATASECTION)
        section = DATA;
      else
      {
        v = split(s, ' ');
        switch (section)
        {
          case(DATA):
            cout << "Inside data section" << endl;
            print_parts(v);
            break;
          case(USER):
            cout << "Inside user section" << endl;
            print_parts(v);
            break;
          default:
            print_parts(v);
        }
      }
    }
  }
  return 0;
}
